package com.macro.mall.tiny.service.Impl;

import com.github.pagehelper.PageHelper;
import com.macro.mall.tiny.common.utils.RedisCache;
import com.macro.mall.tiny.dao.UmsRoleDao;
import com.macro.mall.tiny.mbg.mapper.*;
import com.macro.mall.tiny.mbg.model.*;
import com.macro.mall.tiny.service.UmsRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class UmsRoleServiceImpl implements UmsRoleService {

    @Autowired
    private UmsRoleDao umsRoleDao;
    @Autowired
    private UmsRoleMapper umsRoleMapper;
    @Autowired
    private UmsAdminRoleRelationMapper umsAdminRoleRelationMapper;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private UmsRoleMenuRelationMapper umsRoleMenuRelationMapper;
    @Autowired
    private UmsRolePermissionRelationMapper umsRolePermissionRelationMapper;
    @Autowired
    private UmsRoleResourceRelationMapper umsRoleResourceRelationMapper;

    //根据用户id查询对应菜单信息
    @Override
    public List<UmsMenu> getMenuList(Long userId) {
        return umsRoleDao.getMenuList(userId);
    }

    //根据用户id查询用户角色信息
    @Override
    public List<UmsRole> selectByUserId(Long userId) {
        return umsRoleDao.selectByUserId(userId);
    }

    //添加角色
    @Override
    public Integer create(UmsRole umsRole) {
        umsRole.setAdminCount(0);
        umsRole.setCreateTime(new Date());
        umsRole.setSort(0);
        return umsRoleMapper.insertSelective(umsRole);
    }

    @Override
    public Integer updateById(Long id, UmsRole umsRole) {
        umsRole.setId(id);
        return umsRoleMapper.updateByPrimaryKeySelective(umsRole);
    }

    //批量删除用户角色信息
    @Override
    public Integer delete(List<Long> ids) {
        //删除用户角色表信息
        UmsRoleExample example=new UmsRoleExample();
        UmsRoleExample.Criteria criteria=example.createCriteria();
        criteria.andIdIn(ids);
        Integer code=umsRoleMapper.deleteByExample(example);

        //根据用户角色与用户关联表获取用户id,删除redis中缓存的值
        UmsAdminRoleRelationExample example_2=new UmsAdminRoleRelationExample();
        UmsAdminRoleRelationExample.Criteria criteria_2=example_2.createCriteria();
        criteria_2.andRoleIdIn(ids);
        List<UmsAdminRoleRelation> umsAdminRoleRelationList=umsAdminRoleRelationMapper.selectByExample(example_2);
        umsAdminRoleRelationMapper.deleteByExample(example_2);
        //删除缓存中的值
        for(UmsAdminRoleRelation umsAdminRoleRelation:umsAdminRoleRelationList){
            Long userId=umsAdminRoleRelation.getAdminId();
            redisCache.deleteObject("Userlogin"+userId.toString());
        }

        //删除后台角色菜单关系表数据
        UmsRoleMenuRelationExample example_3=new UmsRoleMenuRelationExample();
        UmsRoleMenuRelationExample.Criteria criteria_3=example_3.createCriteria();
        criteria_3.andRoleIdIn(ids);
        umsRoleMenuRelationMapper.deleteByExample(example_3);

        //删除后台用户角色和权限关系表数据
        UmsRolePermissionRelationExample example_4=new UmsRolePermissionRelationExample();
        UmsRolePermissionRelationExample.Criteria criteria_4=example_4.createCriteria();
        criteria_4.andRoleIdIn(ids);
        umsRolePermissionRelationMapper.deleteByExample(example_4);

        //删除后台角色资源关系表数据
        UmsRoleResourceRelationExample example_5=new UmsRoleResourceRelationExample();
        UmsRoleResourceRelationExample.Criteria criteria_5=example_5.createCriteria();
        criteria_5.andRoleIdIn(ids);
        umsRoleResourceRelationMapper.deleteByExample(example_5);

        return code;
    }

    //获取所有角色信息
    @Override
    public List<UmsRole> getListAll() {
        UmsRoleExample example=new UmsRoleExample();
//        UmsRoleExample.Criteria criteria=example.createCriteria();
        List<UmsRole> umsRoleList=umsRoleMapper.selectByExample(example);
        return umsRoleList;
    }

    //根据角色名称分页获取角色信息
    @Override
    public List<UmsRole> getPageList(String keyword, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        UmsRoleExample example=new UmsRoleExample();
        if(!StringUtils.isEmpty(keyword)){
            UmsRoleExample.Criteria criteria=example.createCriteria();
            criteria.andNameLike("%"+keyword+"%");
        }
        List<UmsRole> umsRoleList=umsRoleMapper.selectByExample(example);

        return umsRoleList;
    }

    //修改角色状态
    @Override
    public Integer updateStatus(Long id, Integer status) {
        UmsRole umsRole=new UmsRole();
        umsRole.setId(id);
        umsRole.setStatus(status);
        return umsRoleMapper.updateByPrimaryKeySelective(umsRole);
    }

    //获取角色相关菜单
    @Override
    public List<UmsMenu> getListMenu(Long roleId) {
        return umsRoleDao.getMenuListByRoleId(roleId);
    }

    //获取角色相关资源
    @Override
    public List<UmsResource> getListResource(Long roleId) {
        return umsRoleDao.getResourceListByRoleId(roleId);
    }

    //给角色分配菜单
    @Override
    public Integer allocMenu(Long roleId, List<Long> menuIds) {
        //先删除原来的关联关系
        UmsRoleMenuRelationExample example=new UmsRoleMenuRelationExample();
        UmsRoleMenuRelationExample.Criteria criteria=example.createCriteria();
        criteria.andRoleIdEqualTo(roleId);
        umsRoleMenuRelationMapper.deleteByExample(example);

        Integer code=0;
        //再插入新的关联关系
        for(Long menuId:menuIds){
            UmsRoleMenuRelation umsRoleMenuRelation=new UmsRoleMenuRelation();
            umsRoleMenuRelation.setRoleId(roleId);
            umsRoleMenuRelation.setMenuId(menuId);
            code+=umsRoleMenuRelationMapper.insertSelective(umsRoleMenuRelation);
        }

        return code;
    }

    //给角色分配资源
    @Override
    public Integer allocResource(Long roleId, List<Long> resourceIds) {
        //先删除原来的关联表数
        UmsRoleResourceRelationExample example=new UmsRoleResourceRelationExample();
        UmsRoleResourceRelationExample.Criteria criteria= example.createCriteria();
        criteria.andRoleIdEqualTo(roleId);
        umsRoleResourceRelationMapper.deleteByExample(example);

        Integer code=0;
        //再批量插入新的关联表数据
        for(Long umsResourceId:resourceIds){
            UmsRoleResourceRelation umsRoleResourceRelation=new UmsRoleResourceRelation();
            umsRoleResourceRelation.setRoleId(roleId);
            umsRoleResourceRelation.setResourceId(umsResourceId);
            code+=umsRoleResourceRelationMapper.insertSelective(umsRoleResourceRelation);
        }

        //删除缓存中的值
        UmsAdminRoleRelationExample example1=new UmsAdminRoleRelationExample();
        UmsAdminRoleRelationExample.Criteria criteria1=example1.createCriteria();
        criteria1.andRoleIdEqualTo(roleId);
        List<UmsAdminRoleRelation> umsAdminRoleRelationList=umsAdminRoleRelationMapper.selectByExample(example1);
        for(UmsAdminRoleRelation umsAdminRoleRelation:umsAdminRoleRelationList){
            String userId=umsAdminRoleRelation.getAdminId().toString();
            redisCache.deleteObject("Userlogin"+userId);
        }

        return code;
    }
}
